package com.ewhale.feitengguest.ui.shop.mine

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.ShopBillDetailDto
import com.ewhale.feitengguest.dto.ShopBillDto
import com.ewhale.feitengguest.dto.ShopWalletDto
import com.ewhale.feitengguest.presenter.shop.mine.ShopWalletPresenter
import com.ewhale.feitengguest.ui.shop.mine.adapter.ShopBillAdapter
import com.ewhale.feitengguest.view.shop.mine.ShopWalletView
import com.ewhale.feitengguest.widget.ScreenBillDialog
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.Dp2PxUtil
import com.simga.library.widget.RecycleViewDivider
import kotlinx.android.synthetic.main.activity_shop_bill.*

class ShopBillActivity : MBaseActivity<ShopWalletPresenter>(), ShopWalletView {
    override fun showWallet(shopWalletDto: ShopWalletDto) {}
    override fun showShopBillDetail(detatil: ShopBillDetailDto){}
    override fun getViewId(): Int {return R.layout.activity_shop_bill}
    override fun isUseBaseTitleBar(): Boolean {return true}

    private val mAdapter by lazy(LazyThreadSafetyMode.NONE) { ShopBillAdapter() }
    private val mDialog by lazy(LazyThreadSafetyMode.NONE) { ScreenBillDialog(mContext) }
    private var pageNumber = 1
    private var status = 3

    override fun init(savedInstanceState: Bundle?) {
        setTitle("全部")
        val drawable = ContextCompat.getDrawable(mContext, R.mipmap.home_arrow_down)
        drawable!!.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        mTvTitle.setCompoundDrawables(null, null, drawable, null)
        list.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
            addItemDecoration(RecycleViewDivider(mContext,
                    Dp2PxUtil.dip2px(mContext, 1f)))
        }
        xloading_view.showLoading()
        presenter.getShopBillList(pageNumber, status)
    }

    override fun showShopBillList(list: List<ShopBillDto>) {
        when (pageNumber) {
            1 -> mAdapter.setNewData(list)
            else -> mAdapter.addData(list)
        }
        if(mAdapter.mData.size == 0){
            xloading_view.showEmpty()
        }else{
            xloading_view.showContent()
        }
        onLoad(list.size)
    }

    override fun initListener() {
        super.initListener()
        ref_layout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                presenter.getShopBillList(pageNumber, status)
            }
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                presenter.getShopBillList(pageNumber, status)
            }
        })
        mTvTitle.setOnClickListener {
            mDialog.showAsDropDown(mRlTitleBar)
        }
        mDialog.setOnClickListener(object : ScreenBillDialog.OnClickListener {
            override fun onClick(type: Int,title:String) {
                setTitle(title)
                status = type
                pageNumber = 1
                xloading_view.showLoading()
                presenter.getShopBillList(pageNumber, status)
            }
        })
        mAdapter.setOnItemClickListener { _, item, _ ->
            ShopBillDetailActivity.open(mContext,item.id)
        }
        xloading_view.setOnRetryClickListener {
            pageNumber = 1
            xloading_view.showLoading()
            presenter.getShopBillList(pageNumber, status)
        }
    }

    private fun onLoad(size: Int) {
        if (ref_layout == null) {
            return
        }
        ref_layout.finishRefreshLoadingMore()
        if (size < 10) {
            ref_layout.enableLoadMore(false)
        } else {
            ref_layout.enableLoadMore(true)
        }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String?, message: String?) {
        showErrorMsg(code, message)
        onLoad(-1)
        if(postCode == 2|| postCode == -10086){
            xloading_view.showError()
        }
    }

    companion object {
        fun open(mContext: MBaseActivity<*>) {
            mContext.startActivity(null, ShopBillActivity::class.java)
        }
    }

}
